package client.advantagesuggestion;

import client.common.result.Result;
import remote.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FeedbackForm extends JFrame {
    
    private JTextField suggestionField;
    private JTextField contactDetailsField;
    private JButton submitButton;
    private UserService userService; // 包含 uploadSuggestion 方法的服务类
    
    public FeedbackForm() {
        setTitle("意见反馈");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new GridLayout(3, 2));
        
        // 标签和输入框
        JLabel suggestionLabel = new JLabel("建议内容:");
        suggestionLabel.setFont(new Font("Dialog", Font.PLAIN, 20)); // 设置字体大小
        suggestionField = new JTextField();
        
        JLabel contactDetailsLabel = new JLabel("联系方式:");
        contactDetailsLabel.setFont(new Font("Dialog", Font.PLAIN, 20)); // 设置字体大小
        contactDetailsField = new JTextField();
        
        // 提交按钮
        submitButton = new JButton("提交");
        submitButton.setPreferredSize(new Dimension(50, 10)); // 设置按钮大小
        submitButton.addActionListener(new SubmitAction());
        
        // 将组件添加到表单中
        add(suggestionLabel);
        add(suggestionField);
        add(contactDetailsLabel);
        add(contactDetailsField);
        add(new JLabel()); // 空单元格
        add(submitButton);
        
        // 初始化 userService
        userService = new UserService() {
        }; // 确保在实际使用中正确初始化
        
        // 设置窗口居中
        setLocationRelativeTo(null);
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        // 设置Nimbus外观
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        SwingUtilities.invokeLater(() -> new FeedbackForm());
    }
    
    // 提交按钮的动作监听器
    private class SubmitAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String suggestion = suggestionField.getText();
            String contactDetails = contactDetailsField.getText();
            
            if (suggestion.isEmpty() || contactDetails.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请填写所有字段。");
                return;
            }
            
            try {
                // 调用 uploadSuggestion 方法
                Result result = userService.uploadSuggestion(suggestion, contactDetails);
                
                // 检查结果并显示相应的消息
                if (result != null && result.isSuccess()) {
                    JOptionPane.showMessageDialog(null, "反馈提交成功！");
                    dispose(); // 关闭当前窗口
                } else {
                    JOptionPane.showMessageDialog(null, "反馈提交失败。");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "提交反馈时发生错误。");
            }
        }
    }
}